﻿using Freejobber.Core.Data;
using System;
using System.Linq;
using System.Transactions;

namespace Freejobber.Core.Generic.Profile
{
    public class UserManager
    {
        private static db_freejobberEntities dbContext=db_freejobberEntities.GetConnecton();
        public enum UserErrorState
        {
            Success,
            DefaultError=1,
            DBConnectionError,
            UserExistInDatabase
        }
        /// <returns>Код ошибки</returns>
        public static UserErrorState CreateNewAccount(string login, string password,string email)
        {
            UserErrorState result=UserErrorState.Success;
            tbl_Users _newUser = null;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead }))
            {
                try
                {
                    #region InitData
                    _newUser = new tbl_Users();
                    _newUser.AdminLevel = 0;
                    _newUser.LoginName = login;
                    _newUser.Password = password;
                    _newUser.Email = email;
                    _newUser.IsEnable =false;
                   // _newUser.ActivationGuid = Guid.NewGuid();
	                #endregion

                    //Создаем аккаунт
                    dbContext.tbl_Users.AddObject(_newUser);
                    dbContext.SaveChanges();
                    try
                    {
                        _newUser = dbContext.tbl_Users.SingleOrDefault(x => x.LoginName == login);
                    }
                    catch (Exception ex)
                    {
                        result = UserErrorState.UserExistInDatabase;
                    }

                    //Создаем профиль
                    tbl_UserProfile _newUserProfile = new tbl_UserProfile();
                    _newUserProfile.UserId = _newUser.Id;
                    dbContext.tbl_UserProfile.AddObject(_newUserProfile);
                    dbContext.SaveChanges();
                    ts.Complete();
                }
                catch (Exception ex)
                {                    
                    result = UserErrorState.DefaultError;
                }
            }
            //MailHelperClass.SendRegisterEmail(_newUser.Email, _newUser.ActivationGuid.ToString()); //Проверка email
            
            return result;
        }

        public static UserClass GetUserByLogin(string login)
        {
            #region Param validation
            if (string.IsNullOrEmpty(login))
                return null;
            #endregion

            var tbl_user = dbContext.tbl_Users.SingleOrDefault(x => x.LoginName == login); //TODO: Исключение
            if (tbl_user == null)
                return null;
            UserClass user = new UserClass(tbl_user.Id);
            return user;
        }

        public static UserClass GetUserByID(int id)
        {
            UserClass user = new UserClass(id);
            return user;
        }

    }
}